package handler;


import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.concurrent.CountDownLatch;

import javax.json.Json;
import javax.json.JsonObject;

/**
 * run this main method and check result 
 */
public class Main {
	private static Logger logger = LoggerFactory.getLogger(Main.class);
	
    public static void main(String args[]) throws Exception {
    	
    	FinanceChainHandler handler = FinanceChainHandler.getInstance();
    	//指定操作org
    	String orgid = "3cc088be7a949c312fd1f40f1a1f91cd411e9e18";
    	
    	String contractData = "{\"contractId\":\"0498c185be4a4162aeac6d9f6e8a579b\","
    			+ "\"supplierId\":\"0498c185be4a4162aeac6d9f6e8a579b\","
    			+ "\"parentEnterpriseId\":\"0498c185be4a4162aeac6d9f6e8a579b\","
    			+ "\"coreFirmId\":\"0498c185be4a4162aeac6d9f6e8a579b\","
    			+ "\"startDate\":\"2019-01-01 00:00:00\","
    			+ "\"expiryDate\":\"2019-12-31 00:00:00\","
    			+ "\"amount\":\"10000000.00\","
    			+ "\"payPeriod\":\"1\","
    			+ "\"finalPeriod\":\"2020-12-25 00:00:00\","
    			+ "\"status\":\"3\","
    			+ "\"reviewTime\":\"2019-03-22 15:07:18\","
    			+ "\"reviewUser\":\"0498c185be4a4162aeac6d9f6e8a579b\","
    			+ "\"reviewUserName\":\"董事长\","
    			+ "\"reviewRemark\":\"审核通过\","
    			+ "\"updateTime\":\"2018-12-20 16:40:15\","
    			+ "\"updateUser\":\"69fce545043111e9b1ac525400f666e0\","
    			+ "\"updateUserName\":\"董事长\","
    			+ "\"createTime\":\"2018-12-20 16:40:15\","
    			+ "\"createUser\":\"d4795f78ff5411e8a77f00163e103bcf\","
    			+ "\"createUserName\":\"董事长\","
    			+ "\"content\":\"合同内容\","
    			+ "\"contractFile\":[{\"uuid\":\"0925c4b517df11e9b1ac525400f666e0\","
    				+ "\"contractId\":\"49e4d0d8632744bfbab8b648c18508ca\","
    				+ "\"path\":\"/images/be7d979dd7a142a0990bf32ede4e5896.png\","
    				+ "\"updateTime\":\"2019-01-14 17:30:31\","
    				+ "\"updateUser\":\"b25d6bbc17c111e9b1ac525400f666e0\","
    				+ "\"hashcode\":\"49e4d0d8632744bfbab8b648c18508ca\","
    				+ "\"updateUserName\":\"董事长\"}]}";
    	
    	JSONObject jo = JSON.parseObject(contractData);
//    	String tranData = handler.saveGenericRecord(orgid,"contract",jo.getString("contractId"),jo.toJSONString());
//    	logger.info(" saveGenericRecord = " + tranData );
    	String queryData = handler.getGenericRecord(orgid,"contract",jo.getString("contractId"));
    	logger.info(" getGenericRecord = " + queryData);
    	

//    	String tranFinancialData = handler.saveFinancialRecord(orgid,jo.getString("contractId"),jo.toJSONString());
//    	logger.info(" saveFinancialRecord = " + tranFinancialData );
    	String queryFinancialData = handler.getFinancialRecord(orgid,jo.getString("contractId"));
    	logger.info(" getFinancialRecord = " + queryFinancialData);    	
    	
    	JSONObject file = jo.getJSONArray("contractFile").getJSONObject(0);
//    	String tranFileData = handler.saveFileRecord(orgid,file.getString("path"),file.getString("hashcode"));
//    	logger.info(" saveFileRecord = " + tranFileData );
    	String queryFileData = handler.getFileRecord(orgid,file.getString("path"));
    	logger.info(" getFileRecord = " + queryFileData);    	    	

    }
    

    public static void LoopInvoke(int loop) throws Exception{
    	FabricHelper helper = FabricHelper.getInstance();
    	for (int i=0; i<loop;i++){
//        	helper.invokeBlockchain("setKeyType", new String[]{"finance","simple"});
    		String[] objarr = new String[]{"finance"};
//    		String[] keyword = new String[]{"supplier0001", "2019", "03"};
//    		String recordContent1 = "{\"supplier_id\":\"supplier0001\", \"fill_date\":\"2019-03\", \"data_1\":\"10000.00\", \"data_2\":\"5000.00\", \"data_3\":\"3000.00\"}";
    		JsonObject ro = Json.createObjectBuilder().add("supplier_id", "supplier0001").add("fill_date", "2019-03").add("data_1", "201912251808").add("data_2", "5000.00").add("data_3", "3000.00").build();

    		String keywords = Json.createArrayBuilder().add(ro.getString("supplier_id")).add(ro.getString("fill_date")).build().toString();
//        	String a=helper.queryBlockchain("getKeyType", objarr);
//        	helper.invokeBlockchain("setKeyType", new String[]{"finance","simple"});
//        	String aa=helper.queryBlockchain("getKeyType", objarr);
//    		String transactionID = helper.invokeBlockchain("saveRecord", ArrayUtils.addAll(objarr, keywords, ro.toString()));
        	String b = helper.queryBlockchain("queryRecord", ArrayUtils.addAll(objarr, keywords));
        	
        	logger.info(" finance = " + b);
//        	logger.info("after KeyType contract = " + a +", KeyType finance = " + b);
    	}
    }
    
    //StartMultiTask(1, 1);
    public static void StartMultiTask(int threadNumber, int loop) throws InterruptedException{
         final CountDownLatch countDownLatch = new CountDownLatch(threadNumber);
         for (int i = 0; i < threadNumber; i++) {
             final int threadID = i;
             new Thread() {
                 public void run() {
                     try {
                    	 LoopInvoke(loop);
                     } catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                     logger.info("threadID:[%s] finished!!", threadID);
                     countDownLatch.countDown();
                 }
             }.start();
         }

         countDownLatch.await();
         logger.info("main thread finished!!");
    }
}


